import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  caseMethodList: {},
  currentCaseMethod: {
    content: "",
    image: "",
    provider: "",
    title: "",
    opt: "",
    video: "",
    cover: "",
    status: 3
  },
  searchCaseMethodCondition: {
    pageSize: 10,
    title: "",
    status: ""
  }
};

const getters = {
  caseMethodList: state => state.caseMethodList,
  currentCaseMethod: state => state.currentCaseMethod,
  searchCaseMethodCondition: state => state.searchCaseMethodCondition
};

const actions = {
  getCaseMethodPageS({ commit }, param) {
    return Api.getCaseMethodPage(param).then(ret => {
      commit(types.UPDATE_CASEMETHOD_LIST, ret.data.data);
      return ret;
    });
  },
  getCaseMethodS({ commit }, id) {
    return Api.getCaseMethod({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_CASEMETHOD, ret.data.data);
      return ret;
    });
  },
  addOrUpdateCaseMethodS({ commit }, payload) {
    return Api.addOrUpdateCaseMethod({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteCaseMethodS({ commit }, param) {
    return Api.deleteCaseMethod(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_CASEMETHOD](state, payload) {
    state.currentCaseMethod = {
      content: "",
      image: "",
      provider: "",
      title: "",
      opt: "",
      video: "",
      cover: "",
      status: 3
    };
  },
  [types.UPDATE_CURRENT_CASEMETHOD](state, payload) {
    state.currentCaseMethod = payload;
  },
  [types.UPDATE_CASEMETHOD_LIST](state, payload) {
    state.caseMethodList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
